<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/9/29
 * Time: 16:24
 */

namespace app\api\controller;



use language\Languagemsg;
use think\Db;

class My extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     * 用户基本信息
     * @return \think\response\Json
     */
    public function baseInfo(){
        $user = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_username, user_image')
            ->find();
        return json(['status' => 200, 'msg' => $user]);
    }
    
    /**
     * 用户信息
     * @return \think\response\Json
     * other_user_id    画师ID
     */
    public function userInfo(){
        $other_user_id = input('other_user_id');
        $is_attent = 1; // 未关注
        $is_apply = 2; // 没有画师请愿，或已达成
        $is_help = 2; // 未助力请愿
        if(isset($other_user_id) && $other_user_id != ''){
            $userid = $other_user_id;
            // 关注
            $my_other = Db::table('up_my_attent')
                ->where('att_my_user_id', $this->user_id)
                ->where('att_other_user_id', $userid)
                ->find();
            $other_my = Db::table('up_my_attent')
                ->where('att_my_user_id', $userid)
                ->where('att_other_user_id', $this->user_id)
                ->find();
            if($my_other){
                $is_attent = 2; // 已关注
            }
            if($my_other && $other_my){
                $is_attent = 3; // 已互关
            }
            // 是否申请了画师请愿
            $painter_apply = Db::table('up_painter_apply')
                ->where('apply_user_id', $userid)
                ->where('apply_type', 3)
                ->where('apply_status', 1)
                ->field('apply_id')
                ->find();
            if($painter_apply){
                $is_apply = 1; // 已有画师请愿，并且没有达成
                // 如果画师有请愿信息，查找我是否帮助请愿过
                $help = Db::table('up_apply_help')
                    ->where('ah_apply_id', $painter_apply['apply_id'])
                    ->where('ah_help_id', $this->user_id)
                    ->find();
                if($help){
                    $is_help = 1;
                }
            }
        }else{
            $userid = $this->user_id;
        }
        $info = Db::table('up_u_user')
            ->where('user_id', $userid)
            ->field('user_username, user_image, user_gender, user_role')
            ->find();
        // 我关注的人
        $info['attent_num'] = $this->myAttent($userid, 2);
        // 我的粉丝
        $info['fans'] = $this->myFans($userid, 2);
        // 关注
        $info['is_attent'] = $is_attent;
        // 画师请愿
        $info['is_apply'] = $is_apply;
        // 是否已经助力
        $info['is_help'] = $is_help;
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 关注/取消关注
     * @return \think\response\Json
     * other_user_id     其他人ID
     */
    public function doAttent(){
        $other_user_id = input('other_user_id');
        $attent = Db::table('up_my_attent')
            ->where('att_my_user_id', $this->user_id)
            ->where('att_other_user_id', $other_user_id)
            ->field('att_id')
            ->find();
        if($attent){
            $res = Db::table('up_my_attent')->delete($attent['att_id']);
            $is_att = 1;
        }else{
            $res = Db::table('up_my_attent')->insert(['att_my_user_id' => $this->user_id, 'att_other_user_id' => $other_user_id, 'att_time' => $this->create_time]);
            $have = Db::table('up_my_attent')
                ->where(['att_my_user_id' => $other_user_id, 'att_other_user_id' => $this->user_id])
                ->find();
            if($have){
                $is_att = 3;
            }else{
                $is_att = 2;
            }
        }
        if($res){
//            return json(['status' => 200, 'msg' => '操作成功', 'is_att' => $is_att]);
            return json(['status' => 200, 'msg' => $this->msg_language->doSuccess[$this->language], 'is_att' => $is_att]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试', 'is_att' => $is_att]);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language], 'is_att' => $is_att]);
        }
    }
    
    /**
     * 画师助力
     * @return \think\response\Json
     * other_user_id     画师ID
     */
    public function helpApply(){
        $other_user_id = input('other_user_id');
        $aready = Db::table('up_apply_help')
            ->where('ah_painter_id', $other_user_id)
            ->where('ah_help_id', $this->user_id)
            ->find();
//        if($aready) return json(['status' => 0, 'msg' => '请不要重复助力']);
        if($aready) return json(['status' => 0, 'msg' => $this->msg_language->notHelpRepet[$this->language]]);
        
        $apply = Db::table('up_painter_apply')
            ->where('apply_user_id', $other_user_id)
            ->where('apply_type', 3)
            ->where('apply_status', 1)
            ->field('apply_id')
            ->find();
        $help = [
            'ah_painter_id' => $other_user_id,
            'ah_help_id' => $this->user_id,
            'ah_apply_id' => $apply['apply_id'],
            'ah_time' => $this->create_time
        ];
        Db::startTrans();
        try{
            // 插入助力表
            Db::table('up_apply_help')->insert($help);
            // 统计助力人数
            $up_num = Db::table('up_other')->where('other_id', 8)->value('other_up_num');
            $help_num = Db::table('up_apply_help')->where('ah_apply_id', $apply['apply_id'])->count();
            if($help_num >= $up_num){
                // 修改请愿状态
                Db::table('up_painter_apply')->where('apply_id', $apply['apply_id'])->update([
                    'apply_status' => 2,
                    'apply_check_time' => $this->create_time
                ]);
                // 修改用户角色
                Db::table('up_u_user')->where('user_id', $other_user_id)->setField('user_role', 6);
            }
            Db::commit();
//            return json(['status' => 200, 'msg' => '助力成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->helpSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 橱窗
     * @return \think\response\Json
     * page     页码
     * other_user_id    画师ID
     */
    public function shopWindow(){
        $other_user_id = input('other_user_id');
        if(isset($other_user_id) && $other_user_id != ''){
            $userid = $other_user_id;
        }else{
            $userid = $this->user_id;
        }
        $goods = Db::table('up_goods')
            ->where('goods_user_id', $userid)
            ->where('goods_type', 1)
            ->field('goods_id, goods_name, goods_thumb, goods_spec_min_price')
            ->order('goods_time desc')
            ->page($this->page, 10)
            ->select();
        if($goods){
            // 商品价格
//            $goods = $this->goodsMinPrice($goods);
            return json(['status' => 200, 'msg' => $goods]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 用户个人信息展示
     * @return \think\response\Json
     */
    public function showInfo(){
        $info = Db::table('up_u_user')
            ->join('up_area ua1', 'ua1.district_id = user_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = user_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = user_qu', 'left')
            ->where('user_id', $this->user_id)
            ->field('user_username, user_image, user_phone, user_email, user_gender, user_birthday, user_sheng, 
            user_shi, user_qu, ua1.district sheng, ua2.district shi, ua3.district qu, user_intro')
            ->find();
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 编辑个人信息
     * @return \think\response\Json
     * user_image      头像
     * user_username   昵称
     * user_phone      手机号
     * user_email      邮箱
     * user_gender     性别
     * user_birthday   生日
     * user_sheng      省
     * user_shi        市
     * user_qu         区
     * user_intro      个人简介
     */
    public function updateUser(){
        $user_image = input('user_image');
        $user_username = input('user_username');
        $user_phone = input('user_phone');
        $user_email = input('user_email');
        $user_gender = input('user_gender');
        $user_birthday = input('user_birthday');
        $user_sheng = input('user_sheng');
        $user_shi = input('user_shi');
        $user_qu = input('user_qu');
        $code = input('code');
        $country_code = input('country_code');
        $user_intro = input('user_intro');
        
        $update['user_update_time'] = $this->create_time;
        if(isset($user_image) && $user_image != '') $update['user_image'] = $user_image;
        if(isset($user_username) && $user_username != '') $update['user_username'] = $user_username;
        if(isset($user_gender) && $user_gender != '') $update['user_gender'] = $user_gender;
        if(isset($user_birthday) && $user_birthday != '') $update['user_birthday'] = $user_birthday;
        if(isset($user_intro) && $user_intro != '') $update['user_intro'] = $user_intro;
        if(isset($user_phone) && $user_phone != ''){
            $exist_phone = Db::table('up_u_user')
                ->where('user_phone', $user_phone)
                ->where('user_id', 'NEQ', $this->user_id)
                ->find();
            if($exist_phone){
//                return json(['status' => 0, 'msg' => '该手机号已被绑定']);
                return json(['status' => 0, 'msg' => $this->msg_language->phoneAreadyBand[$this->language]]);
            }else{
                // 验证码
                $yzm = Db::table('up_phonecode')->where('phone', $user_phone)->where('check_type', 1)->order('time desc')->limit(1)->find();
//                if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
                if($code != $yzm['code']) return json(['status' => 0, 'msg' => $this->msg_language->yzmErr[$this->language]]);
                // 5分钟超时
                $now = time();
//                if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => '验证码失效']);
//                if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
                if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->msg_language->yzmLose[$this->language]]);
                if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->yzmUsed[$this->language]]);
                $update['user_phone'] = $user_phone;
                $update['user_country_code'] = $country_code;
            }
        }
        if(isset($user_email) && $user_email != ''){
            $exist_email = Db::table('up_u_user')
                ->where('user_email', $user_email)
                ->where('user_id', 'NEQ', $this->user_id)
                ->find();
            if($exist_email){
//                return json(['status' => 0, 'msg' => '该邮箱号已被绑定']);
                return json(['status' => 0, 'msg' => $this->msg_language->emailAreadyBand[$this->language]]);
            }else{
                $yzm = Db::table('up_phonecode')->where('email', $user_email)->where('check_type', 2)->order('time desc')->limit(1)->find();
//                if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
                if($code != $yzm['code']) return json(['status' => 0, 'msg' => $this->msg_language->yzmErr[$this->language]]);
                // 5分钟超时
                $now = time();
//                if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => '验证码失效']);
//                if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
                if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->msg_language->yzmLose[$this->language]]);
                if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->yzmUsed[$this->language]]);
                $update['user_email'] = $user_email;
            }
        }
        if(isset($user_qu) && $user_qu != ''){
            $update['user_sheng'] = $user_sheng;
            $update['user_shi'] = $user_shi;
            $update['user_qu'] = $user_qu;
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($update);
        if($res){
            if(isset($code) && $code != ''){
                Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            }
//            return json(['status' => 200, 'msg' => '编辑成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->updateSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 添加收货地址
     * @return \think\response\Json
     * addr_name      用户名称
     * addr_phone     用户手机号
     * addr_sheng     省
     * addr_shi       市
     * addr_qu        区
     * addr_email     邮箱
     * addr_address   详细地址
     * addr_is_default   是否默认    1 默认   2 非默认
     */
    public function addAddr(){
        $addr_name = input('addr_name');
        $addr_phone = input('addr_phone');
        $addr_sheng = input('addr_sheng');
        $addr_shi = input('addr_shi');
        $addr_qu = input('addr_qu');
        $addr_email = input('addr_email');
        $addr_address = input('addr_address');
        $addr_is_default = input('addr_is_default');
        
        $insert = [
            'addr_user_id' => $this->user_id,
            'addr_name' => $addr_name,
            'addr_phone' => $addr_phone,
            'addr_sheng' => $addr_sheng,
            'addr_shi' => $addr_shi,
            'addr_qu' => $addr_qu,
            'addr_email' => $addr_email,
            'addr_address' => $addr_address,
            'addr_is_default' => $addr_is_default,
            'addr_time' => $this->create_time
        ];
        
        Db::startTrans();
        try{
            // 如果是默认，则将之前的默认改成非默认
            if($addr_is_default == 1){
                Db::table('up_user_address')
                    ->where('addr_user_id', $this->user_id)
                    ->where('addr_is_default', 1)
                    ->setField('addr_is_default', 2);
            }
            Db::table('up_user_address')->insert($insert);
            Db::commit();
//            return json(['status' => 200, 'msg' => '添加成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->addSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 收货地址详情
     * @return \think\response\Json
     * addr_id     收货地址ID
     */
    public function addrDetail(){
        $addr_id = input('addr_id');
        $detail = Db::table('up_user_address')
            ->join('up_area ua1', 'ua1.district_id = addr_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = addr_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = addr_qu', 'left')
            ->field('addr_id, addr_name, addr_phone, ua1.district sheng, ua2.district shi, ua3.district qu,
            addr_email, addr_address, addr_is_default, addr_sheng, addr_shi, addr_qu')
            ->where('addr_id', $addr_id)
            ->find();
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 编辑收货地址
     * @return \think\response\Json
     * addr_id         ID
     * addr_name       收货人姓名
     * addr_phone      收货人手机号
     * addr_sheng      省
     * addr_shi        市
     * addr_qu         区
     * addr_email      邮箱
     * addr_address    详细地址
     * addr_is_default 是否默认   1 默认   2 非默认
     */
    public function updateAddr(){
        $addr_id = input('addr_id');
        $addr_name = input('addr_name');
        $addr_phone = input('addr_phone');
        $addr_sheng = input('addr_sheng');
        $addr_shi = input('addr_shi');
        $addr_qu = input('addr_qu');
        $addr_email = input('addr_email');
        $addr_address = input('addr_address');
        $addr_is_default = input('addr_is_default');
    
        $update = [
            'addr_user_id' => $this->user_id,
            'addr_name' => $addr_name,
            'addr_phone' => $addr_phone,
            'addr_sheng' => $addr_sheng,
            'addr_shi' => $addr_shi,
            'addr_qu' => $addr_qu,
            'addr_email' => $addr_email,
            'addr_address' => $addr_address,
            'addr_is_default' => $addr_is_default,
            'addr_updatetime' => $this->create_time
        ];
        $default = Db::table('up_user_address')->where('addr_id', $addr_id)->value('addr_is_default');
    
        Db::startTrans();
        try{
            // 非默认改成默认
            if($addr_is_default == 1 && $default == 2){
                Db::table('up_user_address')
                    ->where('addr_user_id', $this->user_id)
                    ->where('addr_is_default', 1)
                    ->setField('addr_is_default', 2);
            }
            Db::table('up_user_address')->where('addr_id', $addr_id)->update($update);
            Db::commit();
//            return json(['status' => 200, 'msg' => '编辑成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->updateSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 收货地址列表
     * @return \think\response\Json
     */
    public function addrList(){
        $list = Db::table('up_user_address')
            ->join('up_area ua1', 'ua1.district_id = addr_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = addr_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = addr_qu', 'left')
            ->field('addr_id, addr_name, addr_phone, ua1.district sheng, ua2.district shi, ua3.district qu, 
            addr_email, addr_address, addr_is_default')
            ->where('addr_user_id', $this->user_id)
            ->where('addr_del', 1)
            ->order('addr_is_default asc, addr_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除收货地址
     * @return \think\response\Json
     * addr_id     ID
     */
    public function delAddr(){
        $addr_id = input('addr_id');
        $res = Db::table('up_user_address')->where('addr_id', $addr_id)->setField('addr_del', 2);
        if($res){
//            return json(['status' => 200, 'msg' => '删除成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->delSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 设置默认发货地址
     * @return \think\response\Json
     * addr_id      ID
     */
    public function setDefault(){
        $addr_id = input('addr_id');
        $default = Db::table('up_user_address')->where('addr_id', $addr_id)->value('addr_is_default');
        if($default == 1){
//            return json(['status' => 200, 'msg' => '设置成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->setSuccess[$this->language]]);
        }else{
            Db::startTrans();
            try{
                Db::table('up_user_address')->where('addr_user_id', $this->user_id)->where('addr_is_default', 1)->setField('addr_is_default', 2);
                Db::table('up_user_address')->where('addr_id', $addr_id)->setField('addr_is_default', 1);
                Db::commit();
//                return json(['status' => 200, 'msg' => '设置成功']);
                return json(['status' => 200, 'msg' => $this->msg_language->setSuccess[$this->language]]);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        }
    }
    
    /**
     * 我收藏的商品
     * @return \think\response\Json
     * page    页码
     */
    public function myCollect(){
        $collect = Db::table('up_my_collect')
            ->join('up_goods', 'collect_goods_id = goods_id', 'left')
            ->where('collect_my_id', $this->user_id)
            ->field('collect_id, goods_id, goods_name, goods_thumb, goods_spec_min_price, goods_type')
            ->page($this->page, 10)
            ->order('collect_time desc')
            ->select();
        if($collect){
            return json(['status' => 200, 'msg' => $collect]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除我的收藏
     * @return \think\response\Json
     * collect_ids     收藏记录ID  字符串
     */
    public function delCollect(){
        $collect_ids = input('collect_ids');
        $res = Db::table('up_my_collect')->where('collect_id', 'IN', $collect_ids)->delete();
        if($res){
//            return json(['status' => 200, 'msg' => '删除成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->delSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 我的优惠券
     * @return \think\response\Json
     * type    1 未使用   2 已使用   3 已失效
     * page    页码
     */
    public function myCoupon(){
        $type = input('type'); //  1 未使用   2 已使用  3 已失效
        $coupon = Db::table('up_my_coupon')
            ->join('up_coupon', 'coupon_id = mc_coupon_id', 'left')
            ->field('coupon_name, mc_use_end_time, coupon_intro, mc_use_price, mc_dikou_price, 
            coupon_type, mc_use_start_time')
            ->where('mc_user_id', $this->user_id)
            ->where('mc_status', $type)
            ->page($this->page, 15)
            ->order('mc_ling_time desc')
            ->select();
        if($coupon){
            return json(['status' => 200, 'msg' => $coupon]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 是否有审核中的画师/高级画师申请
     * @return \think\response\Json
     * type     1 画师   2 高级画师
     */
    public function haveCheck(){
        $type = input('type');   // 1 申请画师   2 申请高级画师
        $have = Db::table('up_painter_apply')
            ->where('apply_type', $type)
            ->where('apply_status', 1)
            ->where('apply_user_id', $this->user_id)
            ->find();
        if($have){
            $have_painter = 1; // 有审核中的画师/高级画师申请
        }else{
            $have_painter = 2;
        }
        // 是否有高级画师请愿
        $have_apply = Db::table('up_painter_apply')
            ->where('apply_type', 3)
            ->where('apply_user_id', $this->user_id)
            ->find();
        if($have_apply){
            $have_qingy = 1; // 有提交过请愿
        }else{
            $have_qingy = 2;
        }
        return json(['status' => 200, 'msg' => $have_painter, 'have' => $have_qingy]);
    }
    
    /**
     * 画师/高级画师申请/请愿
     * @return \think\response\Json
     * apply_type         申请类型   1 画师   2 高级画师   3 高级画师请愿
     * apply_user_name    姓名
     * apply_user_phone   手机号
     * apply_user_email   邮箱
     * apply_link         作品链接
     * apply_explain      说明
     */
    public function painterApply(){
        $apply_type = input('apply_type'); // 1 画师   2 高级画师   3 高级画师请愿
        $apply_user_name = input('apply_user_name');
        $apply_user_phone = input('apply_user_phone');
        $apply_user_email = input('apply_user_email');
        $apply_link = input('apply_link');
        $apply_explain = input('apply_explain');
        
        if($apply_type != 3){
            $exist = Db::table('up_painter_apply')
                ->where('apply_type', $apply_type)
                ->where('apply_user_id', $this->user_id)
                ->where('apply_status', 1)
                ->find();
            if($exist) return json(['status' => 0, 'msg' => '您有一个请愿正在审核中']);
        }
        
        
        $insert = [
            'apply_user_id' => $this->user_id,
            'apply_user_name' => $apply_user_name,
            'apply_user_phone' => $apply_user_phone,
            'apply_user_email' => $apply_user_email,
            'apply_type' => $apply_type,
            'apply_time' => $this->create_time
        ];
        if($apply_type == 1 || $apply_type == 2){
            $num = date('YmdHis') . rand(0000, 9999);
            $insert['apply_link'] = $apply_link;
            $insert['apply_explain'] = $apply_explain;
            $insert['apply_num'] = $num;
        }
        $apply_id = Db::table('up_painter_apply')->insertGetId($insert);
        if($apply_id){
            return json(['status' => 200, 'msg' => $apply_id, 'apply_time' => $insert['apply_time']]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 用户角色
     * @return \think\response\Json
     */
    public function userRole(){
        $role = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_role');
        return json(['status' => 200, 'msg' => $role]);
    }
    
    /**
     * 画师、高级画师申请列表
     * @return \think\response\Json
     * page     页码
     */
    public function painterApplyList(){
        $list = Db::table('up_painter_apply')
            ->where('apply_user_id', $this->user_id)
            ->where('apply_type', 'IN', '1,2')
            ->field('apply_id, apply_type, apply_time, apply_check_time, apply_status')
            ->order('apply_time desc')
            ->page($this->page, 15)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 申请详情
     * @return \think\response\Json
     * apply_id    申请记录ID
     */
    public function applyDetail(){
        $apply_id = input('apply_id');
        $detail = Db::table('up_painter_apply')
            ->join('up_u_user', 'user_id = apply_user_id', 'LEFT')
            ->where('apply_id', $apply_id)
            ->field('apply_id, apply_user_name, apply_user_phone, apply_user_email, apply_link, apply_num, 
            apply_type, apply_time, apply_check_time, apply_cancel_time, apply_status, apply_false_reason, 
            apply_explain, user_role')
            ->find();
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 修改申请资料
     * @return \think\response\Json
     * apply_id          申请记录ID
     * apply_user_name   申请人姓名
     * apply_user_phone  申请人手机号
     * apply_user_email  申请人邮箱
     * apply_link        作品链接
     * apply_explain     说明
     */
    public function updateApply(){
        $apply_id = input('apply_id');
        $apply_user_name = input('apply_user_name');
        $apply_user_phone = input('apply_user_phone');
        $apply_user_email = input('apply_user_email');
        $apply_link = input('apply_link');
        $apply_explain = input('apply_explain');
    
        $update = [
            'apply_user_name' => $apply_user_name,
            'apply_user_phone' => $apply_user_phone,
            'apply_user_email' => $apply_user_email,
            'apply_link' => $apply_link,
            'apply_explain' => $apply_explain,
            'apply_time' => $this->create_time
        ];
        $res = Db::table('up_painter_apply')->where('apply_id', $apply_id)->update($update);
        if($res){
//            return json(['status' => 200, 'msg' => '提交成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->commitSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 取消申请
     * @return \think\response\Json
     * apply_id     申请记录ID
     */
    public function cancelApply(){
        $apply_id = input('apply_id');
        $res = Db::table('up_painter_apply')->where('apply_id', $apply_id)->setField('apply_status', 4);
        if($res){
//            return json(['status' => 200, 'msg' => '取消成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->cancelSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 我的邀请
     * @return \think\response\Json
     */
    public function myInvite(){
        // 我的邀请码
        $myInvite = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_invite_my_code');
        // 我邀请的人
        $list = Db::table('up_u_user')
            ->where('user_invite_other_code', $myInvite)
            ->field('user_id, user_username, user_time')
            ->order('user_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list, 'myInvite' => $myInvite]);
        }else{
            return json(['status' => 200, 'msg' => [], 'myInvite' => $myInvite]);
        }
    }
    
    /**
     * 高级画师请愿达成人数
     * @return \think\response\Json
     */
    public function qingNum(){
        $num = Db::table('up_other')->where('other_id', 8)->value('other_up_num');
        return json(['status' => 200, 'msg' => $num]);
    }
    
    /**
     * 个人主页背景图
     * @return \think\response\Json
     */
    public function memberImage(){
        $image = Db::table('up_other')->where('other_id', 11)->value('other_zhanwei_image');
        return json(['status' => 200, 'msg' => $image]);
    }
    
    /**
     * 集市图片
     * @return \think\response\Json
     */
    public function jishiImage(){
        $image = Db::table('up_other')->where('other_id', 12)->value('other_zhanwei_image');
        return json(['status' => 200, 'msg' => $image]);
    }
    
    /**
     * 我的关注
     * @param $type    1 列表    2 统计
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    private function myAttent($userid, $type){
        $attent = Db::table('up_my_attent')
            ->join('up_u_user', 'att_other_user_id = user_id', 'left')
            ->where('user_status', 1)
            ->where('att_my_user_id', $userid)
            ->field('att_id, att_other_user_id, user_username, user_image')
            ->order('att_time desc');
        if($type == 1){
            $attent = $attent->page($this->page, 15)->select();
        }else{
            $attent = $attent->count();
        }
        return $attent;
    }
    
    /**
     * 我的粉丝
     * @param $type    1 列表   2 统计
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    private function myFans($userid, $type){
        $fans = Db::table('up_my_attent')
            ->join('up_u_user', 'att_my_user_id = user_id', 'left')
            ->where('user_status', 1)
            ->where('att_other_user_id', $userid)
            ->field('att_id, att_my_user_id, user_username, user_image')
            ->order('att_time desc');
        if($type == 1){
            $fans = $fans->page($this->page, 15)->select();
        }else{
            $fans = $fans->count();
        }
        return $fans;
    }
}